package com.itheima.service;


import com.itheima.common.JsonResult;
import com.itheima.common.JwtUtils;
import com.itheima.mapper.admin.EmployeeMapper;
import com.itheima.pojo.admin.*;
import com.itheima.pojo.threadlocal.AuthThreadLocal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.time.Year;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ChenJunJie
 * @date 2022/6/11 20:35
 * 员工service层
 */
@Service
public class EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;
    @Resource
    private RedisTemplate<String, String> strRedisTemplate;

    //新增员工(注册)
    public boolean employeeAdd(Employee employee) {


        Employee isEmployee = employeeMapper.employeeSelectByUsername(employee);

        if (isEmployee != null) {
            return false;
        }

        int i = employeeMapper.employeeAdd(employee);

        return i > 0;
    }

    //登录
    public JsonResult<Login> login(Employee employee) throws ParseException {

        //加密密码比对
        String password = employee.getPassword();
        String md5Str = DigestUtils.md5DigestAsHex(password.getBytes());
        employee.setPassword(md5Str);

        Login login = employeeMapper.login(employee);

        if (login != null) {

            //判断当前状态
            if (login.getStatus() == 0) {
                return JsonResult.error("当前用户已被禁用");
            }

            //使用jwt工具类创建token并存储在ThreadLocal中
            Map<String, Object> map = new HashMap<>();
            map.put("userName", employee.getUsername());
            String token = JwtUtils.getToken(map);

            login.setToken(token);


            return JsonResult.loginSuccess(login, "登录成功");
        } else {
            return JsonResult.error("登录失败,账号密码有误");
        }
    }

    // //登出
    // public void logout(HttpServletRequest request) {
    //
    //     String token = request.getHeader("Authorization");
    //     if (token != null && !"".equals(token)) {
    //
    //         strRedisTemplate.delete(token);
    //     }
    // }

    // 员工分页查询
    public JsonResult<Pagination<EmployeePagination>> employeePaginationByName(String name, Integer pageNum, Integer pageSize) {


        Integer page = (pageNum - 1) * pageSize;

        List<EmployeePagination> employeeByName = employeeMapper.employeeSelectByName(name);

        List<EmployeePagination> employeePaginations = employeeMapper.employeePaginationByName(name, page, pageSize);

        Pagination<EmployeePagination> eps = new Pagination<EmployeePagination>();
        eps.setRecords(employeePaginations);
        eps.setTotal(employeeByName.size());
        eps.setSize(pageSize);
        eps.setCurrent(pageNum);

        if (employeeByName.size() <= 0 || employeePaginations.size() <= 0) {
            return JsonResult.employeePaginationSuccess(eps, "查询失败,暂无数据");
        }

        return JsonResult.employeePaginationSuccess(eps, "查询成功");

    }

    //通过员工id查询员工
    public EmployeeById employeeSelectById(String id) {
        return employeeMapper.employeeSelectById(id);
    }

    //通过员工id修改员工内容
    public JsonResult<EmployeeById> employeeUpdateById(EmployeeById employeeById) {


        Employee employee = new Employee();
        employee.setUsername(employeeById.getUsername());


        EmployeeById oldEmployeeById = employeeMapper.employeeSelectById(employeeById.getId());

        //判断当前的用户名是否与自己相同，相同则不判断username是否存在。
        if (!oldEmployeeById.getUsername().equals(employeeById.getUsername())) {

            //判断当前修改的username是否存在
            Employee isUsername = employeeMapper.employeeSelectByUsername(employee);
            if (isUsername != null) {
                return JsonResult.error(employeeById.getUsername() + "已存在");
            }
        }


        int i = employeeMapper.employeeUpdateById(employeeById);

        if (i > 0) {
            return JsonResult.success("员工信息修改成功");
        } else {
            return JsonResult.error("员工信息修改失败");
        }
    }


}
